﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraReports.UserDesigner;
using System.IO;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ahi.Base;
using Ahi.Entity;
using Ahi.Config;
using Ahi.Controller;



namespace Ahi.Raporlama
{
    public class SaveCommandHandler : ICommandHandler
    {
        XRDesignPanel panel;
        IdFormParameter id;
        Type dataSourceType;

        public SaveCommandHandler(XRDesignPanel panel, IdFormParameter id, Type dsType)
        {
            this.panel = panel;
            this.id = id;
            this.dataSourceType = dsType;
        }

        public virtual void HandleCommand(ReportCommand command, object[] args, ref bool handled)
        {
            if (!CanHandleCommand(command)) return;

            if (panel.ReportState == ReportState.Changed)
            {

                if ((command == ReportCommand.SaveFile || command == ReportCommand.SaveFileAs) ||
                    (command == ReportCommand.Closing &&
                    XtraMessageBox.Show("Tasarımda değişiklikler var. Kaydetmek istiyor musunuz?", "Kaydet", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
                {
                    RaporEntity entity = null;
                    if (id.HasValue)
                    {
                        entity = MongoAdaptor.Instance.GetDataById<RaporEntity>(id.AsObjectId);
                    }
                    else
                    {
                        string raporAdi = string.Empty;
                        if (GirdiForm.ShowGirdiDialog<string>(ref raporAdi, "Lütfen Rapor Adını Giriniz:") != DialogResult.OK)
                        {
                            handled = true;
                            return;
                        }

                        entity = new RaporEntity
                        {
                            OlusturulmaZamani = DateTime.Now,
                            Isim = raporAdi,
                            VeriKaynagiTipi = dataSourceType != null ? dataSourceType.FullName : string.Empty,
                        };
                    }

                    Save(entity);
                }
            }

            handled = true;
        }

        public virtual bool CanHandleCommand(ReportCommand command)
        {
            return command == ReportCommand.SaveFile ||
                command == ReportCommand.SaveFileAs ||
                command == ReportCommand.Closing;
        }

        void Save(RaporEntity entity)
        {
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    panel.Report.SaveLayout(ms);
                    entity.KaynakKod = Encoding.UTF8.GetString(ms.ToArray());
                    entity.VeriKaynagiTipi = dataSourceType != null ? dataSourceType.FullName : "";
                }
                MongoAdaptor.Instance.Save<RaporEntity>(entity);

                id = entity.Id;
                panel.ReportState = ReportState.Saved;
            }
            catch { throw; }
        }
    }
}
